function toUrl(url,appName){
    window.open(url);
}
//打开添加页面
function add(url) {
	var h = $(window).height() - 100 + 'px';
	var w = $(window).width() - 400 + 'px';
	layer.open({
		type : 2,
		shade : [ 0.5, '#888' ],
		fixed : true,
		title : ' ',
		maxmin : true,
		move : false,
		content : url,
		area : [ w, h ]
	});
}

function errorMsg(msg){
	layer.msg(msg, {
		icon : 5,
		anim : 6,
		shift : 6
	});
}

function successMsg(msg){
	layer.msg(msg, {
		icon : 1
	});
}

function small(url) {
	var h = $(window).height() - 400 + 'px';
	var w = $(window).width() - 700 + 'px';
	layer.open({
		type : 2,
		shade : [ 0.5, '#888' ],
		fixed : true,
		title : ' ',
		maxmin : true,
		move : false,
		content : url,
		area : [ w, h ]
	});
}

function middle(url) {
	var h = $(window).height() - 300 + 'px';
	var w = $(window).width() - 500 + 'px';
	layer.open({
		type : 2,
		shade : [ 0.5, '#888' ],
		fixed : true,
		title : ' ',
		maxmin : true,
		move : false,
		content : url,
		area : [ w, h ]
	});
}

function big(url) {
	var h = $(window).height() - 50 + 'px';
	var w = $(window).width() - 200 + 'px';
	layer.open({
		type : 2,
		shade : [ 0.5, '#888' ],
		fixed : true,
		title : ' ',
		maxmin : true,
		move : false,
		content : url,
		area : [ w, h ]
	});
}

// 满屏
function full(url) {
	var h = $(window).height() + 'px';
	var w = $(window).width() + 'px';
	layer.open({
		type : 2,
		shade : [ 0, '#FFFAFA' ],
		title : false,
		closeBtn : false,
		maxmin : false,
		move : false,
		content : url,
		area : [ w, h ]
	});
}

// 满屏带关闭
function fullWithBar(url) {
	var h = $(window).height() + 'px';
	var w = $(window).width() + 'px';
	layer.open({
		type : 2,
		shade : [ 0.5, '#888' ],
		fixed : true,
		title : ' ',
		maxmin : false,
		move : false,
		content : url,
		area : [ w, h ]
	});
}

function edit(url,height,width) {
	layer.open({
		type : 2,
		shade : [ 0.5, '#888' ],
		fixed : false,
		title : ' ',
		maxmin : false,
		move : false,
		content : url,
		area : [ width, height ]
	});
}
function delWithMsg(url) {
	layer.confirm('确定删除吗？', {
		btn : [ '确定', '取消' ]
	}, function() {
		$.ajax({
			url : encodeURI(url),
			type : "POST",
			success : function(result) {
				if (result.result==1) {
					try {
						flushpage();
					} catch (err) {
						location.reload();
					}
					layer.msg('操作成功！', {
						icon : 1
					});
				} else {
					layer.msg(result.msg, {
						icon : 5,
						anim : 6,
						shift : 6
					});
				}
			}
		});
	}, function() {
	});
}

function childWithMsg(url) {
	layer.confirm('操作确认提示！', {
		btn : [ '确定', '取消' ]
	}, function() {
		$.ajax({
			url : encodeURI(url),
			type : "POST",
			success : function(result) {
				if (result.result==1) {
					try {
						flushpage();
					} catch (err) {
						location.reload();
					}
					layer.msg('操作成功！', {
						icon : 1
					});
				} else {
					layer.msg(result.msg, {
						icon : 5,
						anim : 6,
						shift : 6
					});
				}
			}
		});
	}, function() {
	});
}

// 更具选多选打开添加页面
var loading;
function update(url) {
	$.ajax({
		url : url,
		data : getFormData('#data_form'),
		headers : {
			"Content-Type" : "application/x-www-form-urlencoded"
		},
		type : "POST",
		dataType : "json",
		beforeSend : function() {
			loading = layer.load(2);
		},
		success : function(result) {
			layer.close(loading);
			if (result.result==1) {
				try {
					parent.flushpage();
				} catch (err) {
					parent.location.reload();
				}
				layer.msg('操作成功！', {
					icon : 1
				});
				closelayer();
			} else {
				layer.msg(result.msg, {
					icon : 5,
					anim : 6,
					shift : 6
				});
			}
		},
		error : function(request) {
			layer.close(loading);
			layer.msg("服务器异常，请重试", {
				icon : 5,
				anim : 6,
				shift : 6
			});
		}
	});
}

function add(url) {
	$.ajax({
		url : url,
		data : getFormData('#data_form'),
		headers : {
			"Content-Type" : "application/x-www-form-urlencoded"
		},
		type : "POST",
		dataType : "json",
		beforeSend : function() {
			loading = layer.load(2);
		},
		success : function(result) {
			layer.close(loading);
			if (result.result==1) {
				layer.msg('操作成功！', {
					icon : 1
				});
				try {
					parent.flushpage();
				} catch (err) {
					parent.location.reload();
				}
				//closelayer();
			} else {
				layer.msg(result.msg, {
					icon : 5,
					anim : 6,
					shift : 6
				});
			}
		},
		error : function(request) {
			layer.close(loading);
			layer.msg("服务器异常，请重试", {
				icon : 5,
				anim : 6,
				shift : 6
			});
		}
	});
}

// 满屏带关闭,传递内容
function fullWithBarContent(obj) {
	var h = $(window).height() + 'px';
	var w = $(window).width() + 'px';
	layer.open({
		type : 1,
		shade : [ 0.5, '#888' ],
		fixed : true,
		title : ' ',
		maxmin : false,
		move : false,
		content : obj,
		area : [ w, h ]
	});
}

// 关闭当前层
function closelayer() {
	var index = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引
	parent.layer.close(index); // 再执行关闭
}

function toUtf8(str) {
	var out, i, len, c;
	out = "";
	len = str.length;
	for (i = 0; i < len; i++) {
		c = str.charCodeAt(i);
		if ((c >= 0x0001) && (c <= 0x007F)) {
			out += str.charAt(i);
		} else if (c > 0x07FF) {
			out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
			out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
			out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
		} else {
			out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
			out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
		}
	}
	return out;
}

//创建一个转化的方法
function getFormData(formId){
    if(formId){
       //首先获取表单的序列化数组
       var arr = $(formId).serializeArray();
       if(arr && arr.length > 0){
         var d = {};//创建一个空的对象 以便装数组数据
         $.each(arr,function(index,it){//index为数组的下标 it为数组对应的数据对象 
           if(it.value){//判断值是否为空
               if(d[it.name]){//判断值是否有同名的多个数据 
                   d[it.name] = d[it.name] +","+ it.value;

               }else{
                   d[it.name] = it.value;
               }
           }
         })
         return d ; // 返回已经拼装好的对象
       }
    }
    return {};//如果不存在此id则传出一个空的对象
}
//格式化时间
function format(time, format) {
    var t = new Date(time);
    var tf = function (i) {
        return (i < 10 ? '0' : '') + i
    };
    return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function (a) {
        switch (a) {
            case 'yyyy':
                return tf(t.getFullYear());
                break;
            case 'MM':
                return tf(t.getMonth() + 1);
                break;
            case 'mm':
                return tf(t.getMinutes());
                break;
            case 'dd':
                return tf(t.getDate());
                break;
            case 'HH':
                return tf(t.getHours());
                break;
            case 'ss':
                return tf(t.getSeconds());
                break;
        }
    })
}

//在新窗口打开链接
function openUrl(url){
	if(!url) return;
	window.open(url,"_blank");
}

//红色字体错误提示
function errorMsgRed(msg){
	msg = '<a style=\'color:red\'>' + msg + '</a>';
	layer.msg(msg , {
		icon : 5,
		anim : 6,
		shift : 6
	});
};

//黑色字体成功提示
function successMsgBlack(msg){
	msg = '<a style=\'color:black\'>' + msg + '</a>';
	layer.msg(msg, {
		icon : 1
	});
};

//字符串的replaceAll
String.prototype.replaceAll = function(s1, s2) {
    return this.replace(new RegExp(s1, "gm"), s2);
}